package com.yl.ydk.presenter.impl

import android.content.Context
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.LiveDetailBean
import com.yl.ydk.presenter.contract.LiveDetailContract
import com.yl.ydk.utils.GlideUtils
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class LiveDetailImpl : BasePresenter<LiveDetailContract.View>(),
    LiveDetailContract.Presenter<LiveDetailContract.View> {


    override fun getLiveDetail(context: Context, vararg s: String) {

        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getLiveDetail(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<LiveDetailBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: LiveDetailBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {
                            mView?.let { mView ->
                                GlideUtils.loadToBackground(
                                    context,
                                    t.data.image,
                                    mView.getIvImage()
                                )
                                GlideUtils.loadRound(context, t.data.user.photo, mView.getIvHead())
                                mView.getTvName().text = t.data.title
                                mView.getTvDName().text =
                                    t.data.user.name + "  " + t.data.user.nickname
                                mView.getTvDesc().text = t.data.desc
                                mView.getTvStartTime().text = t.data.start_time
                                mView.getPushUrl(t.data.play_url.rtmp)
                                mView.getLiveDetailBean(t)
                            }
                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }

}